package org.filecollector.msgcontroller.requests;

import com.google.gson.Gson;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

public class SerializationTest {
    static Gson gson = new Gson();

    RequestMessage getRequest() {
        return new RequestMessage(
                RESTMethod.GET,
                "https://smt.smt/a?as=as#agjg%15%ff%a1!$&:[]@'()+*,;=",
                15L,
                66,
                "stuff"
        );
    }

    @Before
    @Test
    public void deserializationRMTest() {
        RequestMessage innerObj = getRequest();
        String innerMsg = String.format(
                "{\"method\":\"%s\",\"uri\":%s,\"id\":%d,\"buf_size\":%d,\"data\":\"%s\"}",
                innerObj.method(),
                gson.toJson(innerObj.uri()),
                innerObj.id(),
                innerObj.bufSize(),
                innerObj.data()
        );
        TestCase.assertEquals(RequestMessage.deserialize(innerMsg), innerObj);
    }

    @Test
    public void deserializationSRMTest() {
        RequestMessage innerObj = getRequest();
        String innerMsg = String.format(
                "{\"method\":\"%s\",\"uri\":%s,\"id\":%d,\"buf_size\":%d,\"data\":\"%s\"}",
                innerObj.method(),
                gson.toJson(innerObj.uri()),
                innerObj.id(),
                innerObj.bufSize(),
                innerObj.data()
        );
        SyncRequestMessage obj = new SyncRequestMessage(100L, innerObj);
        String msg = String.format(
                "{\"operation_id\":%d,\"request\":%s}",
                obj.operationID(),
                innerMsg
        );
        TestCase.assertEquals(SyncRequestMessage.deserialize(msg), obj);
    }

    @Before
    @Test
    public void serializationRMTest() {
        RequestMessage innerObj = getRequest();
        String innerMsg = String.format(
                "{\"method\":\"%s\",\"uri\":%s,\"id\":%d,\"buf_size\":%d,\"data\":\"%s\"}",
                innerObj.method(),
                gson.toJson(innerObj.uri()),
                innerObj.id(),
                innerObj.bufSize(),
                innerObj.data()
        );
        TestCase.assertEquals(innerMsg, innerObj.serialize());
    }

    @Test
    public void serializationSRMTest() {
        RequestMessage innerObj = getRequest();
        String innerMsg = String.format(
                "{\"method\":\"%s\",\"uri\":%s,\"id\":%d,\"buf_size\":%d,\"data\":\"%s\"}",
                innerObj.method(),
                gson.toJson(innerObj.uri()),
                innerObj.id(),
                innerObj.bufSize(),
                innerObj.data()
        );
        SyncRequestMessage obj = new SyncRequestMessage(100L, innerObj);
        String msg = String.format(
                "{\"operation_id\":%d,\"request\":%s}",
                obj.operationID(),
                innerMsg
        );
        TestCase.assertEquals(msg, obj.serialize());
    }
}
